
package com.hyst.seviceImpl;

import com.hyst.domain.CurriculumSchedule;
import com.hyst.domain.CurriculumScheduleView;
import com.hyst.repository.CurriculumScheduleMapper;
import org.activiti.engine.impl.util.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.domain.PurchaseCourseView;
import com.hyst.repository.PurchaseCourseViewMapper;
import com.hyst.service.PurchaseCourseViewService;
import com.hyst.utils.UuidTool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Date：2018年08月31日 
 * @author： dy 
 * @version ：1.0 1.[2018年08月31日]
 */
@Service
public class PurchaseCourseViewServiceImpl implements PurchaseCourseViewService {

	@Autowired
	private PurchaseCourseViewMapper mapper;

	@Autowired
	private CurriculumScheduleMapper curriculumScheduleMapper;

	@Override
	public Page<PurchaseCourseView> list(final PurchaseCourseView purchaseCourseView) {
		final Page<PurchaseCourseView> page = PageHelper.startPage(purchaseCourseView.getPageNum(), purchaseCourseView.getPageSize());
		mapper.select(purchaseCourseView);

		for(PurchaseCourseView purchaseCourseView1:page.getResult()){
			Map<String,String> sqlMap = new HashMap<>();
			sqlMap.put("lessionId",purchaseCourseView1.getMaterial());
			sqlMap.put("leveId",purchaseCourseView1.getMaterialLevel());
			String lesson=mapper.getLesson(sqlMap);
			purchaseCourseView1.setNameEn(lesson);
		}
		return page;
	}

	@Override
	public PurchaseCourseView getById(final PurchaseCourseView purchaseCourseView) {
        PurchaseCourseView purchaseCourseView1= mapper.selectByPrimaryKey(purchaseCourseView);
//        String lesson = mapper.getSelect2Data(purchaseCourseView1.getMaterialLevel()+"");
//        purchaseCourseView1.setLevelName(lesson);
        return purchaseCourseView1;
	}

	@Override
	public PurchaseCourseView insert(final PurchaseCourseView purchaseCourseView) {
		purchaseCourseView.setUniqueid(UuidTool.randomUUID());
		mapper.insertSelective(purchaseCourseView);
		return purchaseCourseView;
	}

	@Override
	public PurchaseCourseView getByPriKey(final String uniqueid) {
		final PurchaseCourseView purchaseCourseView = mapper.selectByPrimaryKey(uniqueid);

		Map<String,String> sqlMap = new HashMap<>();
		sqlMap.put("lessonId",purchaseCourseView.getMaterial());
		sqlMap.put("levelId",purchaseCourseView.getMaterialLevel());
        String lesson = mapper.getSelect2Data(sqlMap);
        purchaseCourseView.setLevelName(lesson);

		CurriculumSchedule curriculumSchedule = new CurriculumSchedule();
		curriculumSchedule.setPurchaseCourseId(uniqueid);
		List<CurriculumScheduleView> byTeacherIdAndDate = curriculumScheduleMapper.getViewByPurchaseCourse(curriculumSchedule);

		int count = 0;
		if(byTeacherIdAndDate.size()>0){
			for (CurriculumScheduleView view : byTeacherIdAndDate) {
				if (view.getState() !=null && (view.getState() == 7 || view.getState()==3) ) {
					count += view.getClassLength();
				}
			}
		}
		purchaseCourseView.setHoursCompleted(count);
		return purchaseCourseView;
	}

	@Override
	public PurchaseCourseView updateSelective(final PurchaseCourseView purchaseCourseView) {
		mapper.updateByPrimaryKeySelective(purchaseCourseView);
		return purchaseCourseView;
	}

	@Override
	public PurchaseCourseView update(final PurchaseCourseView purchaseCourseView) {
		mapper.updateByPrimaryKey(purchaseCourseView);
		return purchaseCourseView;
	}

	@Override
	public PurchaseCourseView delete(final String uniqueid) {

		return null;
	}

}
